import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_tracker/flutter_tracker.dart';
import 'package:flutter_tracker/widget/qws_elevated_button.dart';
import 'package:uuid/uuid.dart';

import 'PinYinTextEditController.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _HomePage();
  }

}

class _HomePage extends State<HomePage> {
  var uuid = const Uuid();
  int lastKeyboardDown = 0;
  var lastInput = "";
  late TextField _textField;
  final StreamController<String> streamController = StreamController.broadcast();
  Pinyintexteditcontroller controller = Pinyintexteditcontroller();
  final FocusNode _focusNode = FocusNode(onKeyEvent: (node, event) {
    print("onKeyEvent : ${event.character}");
    if (event.character == 'a') {
      return KeyEventResult.handled;
    }
    return KeyEventResult.ignored;
  });

  @override
  void initState() {
    _textField = TextField(
      focusNode: _focusNode,
      controller: controller,
      keyboardType: TextInputType.emailAddress,
    );
    controller.addListener(() {
      if (lastInput != controller.completeText) {
        lastInput = controller.completeText;
        ///通知onChanged
        _textField.onChanged!(controller.completeText);
      }
    });

    super.initState();
  }

  Future<bool?> showDeleteConfirmDialog() {
    return showDialog<bool>(
      context: context,
      routeSettings: const RouteSettings(
        name: "tips"
      ),
      builder: (context) {
        return AlertDialog(
          title: const Text("提示"),
          content: const Text("您确定要删除当前文件吗?"),
          actions: <Widget>[
            TextButton(
              child: const Text("取消"),
              onPressed: () => Navigator.of(context).pop(), // 关闭对话框
            ),
            TextButton(
              child: const Text("删除"),
              onPressed: () {
                //关闭对话框并返回true
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            QwsElevatedButton(
              onPressed: () async{
                // context.go("/detail");
                showDeleteConfirmDialog();
              },
              eventId: "to_detail",
              eventType: "click",
              child: const Text("INSERT"),
            ),
            QwsElevatedButton(
              onPressed: () {
                FlutterTracker.instance.read();
              },
              eventId: "click_read",
              eventType: "click",
              child: const Text("READ"),
            ),
            QwsElevatedButton(
              onPressed: () {
                FlutterTracker.instance.clear();
              },
              eventId: "click_clear",
              eventType: "click",
              child: const Text("CLEAR"),
            ),
            SizedBox(
              width: 360,
              child: _textField,
            ),
          ],
        ),
      ),
    );
  }
}
